/**
 * 动态网站开发
 * 
 * 成绩查询功能
 */
const http = require('http');
const path = require('path');
const querystring = require('querystring');
const template = require('art-template');
const scoreData = require('./public/scores.json');

http.createServer((req, res) => {
    //路由（请求路径+请求方式）
    //查询成绩的入口地址 /query
    if (req.url.startsWith('/query') && req.method == 'GET') {
        let content = template(path.join(__dirname, 'view', 'index.art'), {});
        res.end(content);
    } else if (req.url.startsWith('/score') && req.method == 'POST') {
        //获取成绩的结果     /score
        let pdata = '';
        req.on('data', (chunk) => {
            pdata += chunk;
        });
        req.on('end', () => {
            let obj = querystring.parse(pdata);
            let result = scoreData[obj.code];
            console.log(result);
            let content = template(path.join(__dirname, 'view', 'result.art'), result);
            res.end(content);
        });
    } else if (req.url.startsWith('/findAll') && req.method == 'GET') {

        let arr = [];

        for (const key in scoreData) {
            if (scoreData.hasOwnProperty(key)) {
                arr.push(scoreData[key]);
            }
        }

        //全部成绩
        let content = template(path.join(__dirname, 'view', 'list.art'), {
            list: arr
        });
        res.end(content);
    }


}).listen(3000, () => {
    console.log('running........');
});